package com.psm.controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.psm.interceptor.IndexInterceptor;
import com.psm.model.DataGrid;
import com.psm.model.SysUser;
import com.psm.util.MD5;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheam on 15/6/19.
 */
@Before(IndexInterceptor.class)
public class UserController extends BaseController<SysUser> {
    public Logger log=Logger.getLogger(getClass());

    public void index(){

        render("index.html");
    }


    public void listJson(){

        Record user = getCurrentUser();

        List<Object> param = new ArrayList<Object>();
        StringBuffer where = new StringBuffer();
        /** 添加查询字段条件*/
        String sortName = this.getPara("sort");
        String sortOrder = this.getPara("order", "desc");

        String code = this.getPara("code", "");
        String name = this.getPara("name", "");
        String dep_id = this.getPara("dep_id", "");

        where.append(" and FIND_IN_SET(t.dep,getDepChildList("+user.get("dep")+"))");

        if(!"".equals(code)){
            where.append(" and t.code like '%"+code+"%'");
        }

        if(!"".equals(name)){
            where.append(" and t.name like '%"+name+"%'");
        }
        if(!"".equals(dep_id)){
            where.append(" and FIND_IN_SET(t.dep,getDepChildList("+dep_id+"))");
        }

        if (sortName != null) {
            where.append(" order by ");
            where.append(sortName);
            where.append(" ");
            where.append(sortOrder);
        }

        Page<Record> p = Db.paginate(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), "select t.*,d.dep_name,su.name create_user_name,c.name state,(select GROUP_CONCAT(r.name)  from sys_role r,sys_ref_user_role ur where r.id=ur.sys_role_id and ur.sys_user_id=t.id) role_name ", "from " + SysUser.tableName + " t left join sys_department d on t.dep=d.id left join sys_user su on t.create_user=su.id left join sys_pa_code c on c.id=t.status  where 1=1 " + where.toString(), param.toArray());
        DataGrid dg = new DataGrid();
        List<Record> re_list = new ArrayList<Record>();

        for(Record l:p.getList()){

        }
        dg.setRows(p.getList());
        dg.setTotal(p.getTotalRow());

        renderJson(dg);

    }

    public void listTeacherJson(){

        Record user = getCurrentUser();

        List<Object> param = new ArrayList<Object>();
        StringBuffer where = new StringBuffer();
        /** 添加查询字段条件*/
        String sortName = this.getPara("sort");
        String sortOrder = this.getPara("order", "desc");

        where.append(" and FIND_IN_SET(t.dep,getDepChildList("+user.get("dep")+"))");
        where.append(" and exists(select 1 from sys_ref_user_role where sys_role_id=5 and sys_user_id=t.id)");

        if (sortName != null) {
            where.append(" order by ");
            where.append(sortName);
            where.append(" ");
            where.append(sortOrder);
        }

        Page<Record> p = Db.paginate(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), "select t.*,d.dep_name,su.name create_user_name,c.name state ", "from " + SysUser.tableName + " t left join sys_department d on t.dep=d.id left join sys_user su on t.create_user=su.id left join sys_pa_code c on c.id=t.status  where 1=1 " + where.toString(), param.toArray());
        DataGrid dg = new DataGrid();
        dg.setRows(p.getList());
        dg.setTotal(p.getTotalRow());

        renderJson(dg);
    }

    public void listAreaAdminJson(){

        Record user = getCurrentUser();

        List<Object> param = new ArrayList<Object>();
        StringBuffer where = new StringBuffer();
        /** 添加查询字段条件*/
        String sortName = this.getPara("sort");
        String sortOrder = this.getPara("order", "desc");

        where.append(" and exists(select 1 from sys_ref_user_role where sys_role_id=4 and sys_user_id=t.id)");

        if (sortName != null) {
            where.append(" order by ");
            where.append(sortName);
            where.append(" ");
            where.append(sortOrder);
        }

        Page<Record> p = Db.paginate(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), "select t.*,d.dep_name,su.name create_user_name,c.name state ", "from " + SysUser.tableName + " t left join sys_department d on t.dep=d.id left join sys_user su on t.create_user=su.id left join sys_pa_code c on c.id=t.status  where 1=1 " + where.toString(), param.toArray());
        DataGrid dg = new DataGrid();
        dg.setRows(p.getList());
        dg.setTotal(p.getTotalRow());

        renderJson(dg);
    }

    public void listDepAdminJson(){

        Record user = getCurrentUser();

        List<Object> param = new ArrayList<Object>();
        StringBuffer where = new StringBuffer();
        /** 添加查询字段条件*/
        String sortName = this.getPara("sort");
        String sortOrder = this.getPara("order", "desc");

        where.append(" and t.dep="+user.get("dep").toString()+" and exists(select 1 from sys_ref_user_role where sys_role_id=6 and sys_user_id=t.id)");

        if (sortName != null) {
            where.append(" order by ");
            where.append(sortName);
            where.append(" ");
            where.append(sortOrder);
        }

        Page<Record> p = Db.paginate(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), "select t.*,d.dep_name,su.name create_user_name,c.name state,(select GROUP_CONCAT(r.name)  from sys_role r,sys_ref_user_role ur where r.id=ur.sys_role_id and ur.sys_user_id=t.id) role_name ", "from " + SysUser.tableName + " t left join sys_department d on t.dep=d.id left join sys_user su on t.create_user=su.id left join sys_pa_code c on c.id=t.status  where 1=1 " + where.toString(), param.toArray());
        DataGrid dg = new DataGrid();
        dg.setRows(p.getList());
        dg.setTotal(p.getTotalRow());

        renderJson(dg);
    }

    public void edit() {
        try {
            Long id = getParaToLong("id");
            Record user = SysUser.me.queryEditUser(id);
            this.setAttr("po",user);
        } catch (Exception e) {
            log.error("编辑异常", e);
        }
    }

    public void save(){
         String id = getPara("id","");
         String code = getPara("code","");
         String name = getPara("name","");
         String email = getPara("email","");
         String phone = getPara("phone","");
         String password = getPara("password","");
         String dep = getPara("dep", "");
         String status = getPara("status", "0");

         SysUser check_user = SysUser.me.findFirst("select * from " + SysUser.tableName +" where code=?",code);
            if(null != check_user && !check_user.get("id").toString().equals(id)){
                this.rendJson_(0, "帐户已存在");
                return;
            }



        SysUser user = null;
        if("".equals(id)){
             user = new SysUser();
        }else{
             user = SysUser.me.findById(id);
        }
        user.set("code",code);
        user.set("name",name);
        user.set("email",email);
        user.set("phone",phone);
        user.set("dep",dep);
        if(!"".equals(password)){
            user.set("pwd", MD5.getMD5ofStr(password));
        }
        user.set("status",status);

        if("".equals(id)){
            user.save();
        }else{
            user.update();
        }

        this.rendJson_(1, "操作成功");
    }
    
    public void detele(){
    	String id = getPara("uid", "");
        SysUser.me.deleteById(id);

    	this.rendJson_(1, "操作成功");
    }

}
